package com.huiying.mapper;

import com.huiying.entity.entity.Like;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Author: lsh
 * @Date: 2024/11/18/15:36
 * @Description: 点赞表数据访问类
 */
@Mapper
public interface LikeMapper {

    @Select("select * from likes where content_id = #{demandId} and user_id = #{userId}")
    Like getByDemandIdAndUserId(@Param("demandId") Long demandId,
                                @Param("userId") Long userId);

    @Insert("insert into likes(user_id, content_id, content_type, create_time) " +
            "values (#{userId},#{contentId},#{contentType},#{createTime})")
    void insertLike(Like like);

    @Select("select user_id from likes where content_id = #{demandId}")
    List<Long> getUserIdsByDemandId(Long demandId);

    @Delete("delete from likes where content_id = #{demandId} and user_id = #{userId}")
    void deleteByDemandIdAndUserId(@Param("demandId") Long demandId,
                                   @Param("userId") Long userId);

    @Delete("delete from likes where content_type = #{demand} and content_id = #{demandId}")
    void deleteByDemandId(@Param("demandId") Long demandId,
                          @Param("demand") String demand);

    @Select("select user_id from likes where content_id = #{contentId} and content_type = #{contentType}")
    List<Long> getUserIdsByContentId(@Param("contentId") Long contentId,
                                     @Param("contentType") String contentType);


    List<Long> getIdsByContentIdsAndUsers(Long contentId, String contentType,List<Long> likesToDelete);

    @Delete("delete from likes where content_id = #{contentId} and content_type = #{contentType} and user_id = #{userId}")
    void deleteByContentIdAndUserId(@Param("contentId") Long contentId,
                                    @Param("contentType") String contentType,
                                    @Param("userId") Long userId);

    @Select("select count(*) from likes where content_id = #{contentId} and content_type = #{contentType}")
    long countLikesByContentId(@Param("contentId") Long contentId,
                               @Param("contentType") String contentType);

    void batchInsertLikes(List<Like> newLikes);

    void batchDelete(List<Long> likesToDelete);
}
